package com.woniu.car.controller;
import com.woniu.car.annotation.LoginCode;
import com.woniu.car.annotation.TelAndPwdCheck;
import com.woniu.car.annotation.TelCheck;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.config.MyRedisTemplate;
import com.woniu.car.dto.TelCodeDto;
import com.woniu.car.dto.UserAccountDto;
import com.woniu.car.dto.UserRegisterDto;
import com.woniu.car.exception.AccountException;
import com.woniu.car.param.*;
import com.woniu.car.service.TUserAccountService;
import com.woniu.car.util.Code;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WWX
 * @since 2021-06-19
 */
@Api(tags = "用户账号控制器")
@RestController
@RequestMapping("/tUserAccount")
public class TUserAccountController {
    @Resource
    private MyRedisTemplate myRedisTemplate;
    @Resource
    private TUserAccountService tUserAccountService;


    /**
     *@Author 王维鑫
     *@Description 发送短信
     *@Param
     *@return
     **/
    @TelCheck
    @ApiOperation("发送短信接口")
    @GetMapping("sendCode")
    public ResponseEntity<TelCodeDto> sendCode(CodeTelParam codeTelParam) throws Exception {
        TelCodeDto telCodeDto = tUserAccountService.sendCode(codeTelParam.getTel());
        return  ResponseEntity.builder(TelCodeDto.class).setObject(telCodeDto).setCode(Code.NORMAL).setMsg("验证码发送成功");
    }


    /**
     *@Author 王维鑫
     *@Description 用户注册
     *@Param
     *@return
     **/
    @ApiOperation("注册接口")
    @TelAndPwdCheck
    @PostMapping("register")
    public ResponseEntity registerUserAccount(UserRegisterParam param)  {
        UserRegisterDto userAccountDto = tUserAccountService.registerUser(param);
        return ResponseEntity.builder(UserRegisterDto.class).setObject(userAccountDto).setCode(Code.NORMAL).setMsg("注册成功");
    }


    /**
     *@Author 王维鑫
     *@Description 短信登录
     *@Param
     *@return
     **/
    @ApiOperation("短信登录接口")
    @LoginCode
    @GetMapping("loginByCode")
    public ResponseEntity loginUserAccount(UserLoginByCodeParam param)  {
        UserAccountDto userAccountDto = tUserAccountService.loginByCode(param);
        return ResponseEntity.builder(UserAccountDto.class).setObject(userAccountDto).setCode(Code.NORMAL).setMsg("登录成功");
    }

    /**
     *@Author 王维鑫
     *@Description 账号密码登录
     *@Param
     *@return
     **/
    @TelAndPwdCheck
    @ApiOperation("账号密码登录接口")
    @PostMapping("loginByPwd")
    public ResponseEntity loginByPwd(UserLoginByPwdParam param)  {
        UserAccountDto userAccountDto = tUserAccountService.loginByPwd(param);
       return ResponseEntity.builder(UserAccountDto.class).setObject(userAccountDto).setCode(Code.NORMAL).setMsg("登录成功");
    }
    /**
     *@Author 王维鑫
     *@Description 根据用户ID查询用户
     *@Param
     *@return
     **/
    @ApiOperation("根据用户ID查询用户")
    @GetMapping("queryUserById")
    public ResponseEntity queryUserById(Integer userId){
        System.out.println("进入方法");
        UserAccountDto userAccountDto = tUserAccountService.queryUserById(userId);
        if(null==userAccountDto){
            throw new AccountException("未查询到用户");
        }
        return ResponseEntity.builder(UserAccountDto.class).setObject(userAccountDto).setCode(Code.NORMAL).setMsg("查询成功");
    }










}

